"use client";
import { useRouter } from "next/navigation";
import { toast } from "react-hot-toast";
import { signOut } from "next-auth/react"; // 引入 signOut 方法
import styles from "./button.module.css";

const LogoutButton = ({ text }) => {
  const router = useRouter();

  const handleLogout = async () => {
    try {
      // 使用 NextAuth 的 signOut 方法清除 session
      await signOut({
        redirect: false, // 不自动跳转
      });
      toast.success("已成功注销");
      router.push("/dashboard/login");
      router.refresh(); // 刷新页面，更新状态
    } catch (err) {
      toast.error("注销失败");
    }
  };

  return (
    <button
      onClick={handleLogout}
      className={styles.button}
    >
      {text}
    </button>
  );
};

export default LogoutButton;